/*
    Copyright (c) Guangzhou Data Control Network Technology Co., Ltd. All rights reserved.
    Licensed under the MIT License.
    contact:dcdev_founder@foxmail.com
*/
//监听手机屏幕翻转
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", resize_form, false);

function resize_form() {
    if (browser_info.is_mobile) {
        $("#_dc_ui_browser_mask_").css("width", "100%").css("height", "100%");
        // 关闭按钮位置 
        $(`#_dc_ui_browser_close_`).css("left", $(`#_dc_ui_browser_container_`).width() / 2 - 18).css("top", $(`#_dc_ui_browser_container_`).height());
    }
}

function go_back() {
    fram1.contentWindow.history.go(-1);
}

function go_for() {
    fram1.contentWindow.history.go(1);
}

function setStatus() {
    var currentPagNum = fram1.contentWindow.history.position;
    var maxPagNum = fram1.contentWindow.history.length;
    if (currentPagNum > 0) {
        backBut.style.display = "block";
    } else {
        backBut.style.display = "none";
    }
    if (currentPagNum < maxPagNum - 1) {
        goBut.style.display = "block";
    } else {
        goBut.style.display = "none";
    }
}

ui_browser_class = {
    // 显示loading
    init: function (data) {
        {
            ui_class.show_loading();
            window["_dc_ui_browser_data_"] = data;
            var width = "";
            var height = "";
            if (browser_info.is_desktop) {
                width = data.appear.pc.size.w;
                height = data.appear.pc.size.h;
            }
            else {
                width = data.appear.mobile.size.w;
                height = data.appear.mobile.size.h;
            }
            var html = `
<div id="_dc_ui_browser_mask_" class="_dcdev_preset_" style="display:none;position:fixed;z-index:99999;top:0;left:0;width:100%;height:100%;background-color:black;opacity:0;display:none;"></div>
<div id="_dc_ui_browser_container_" class="_dcdev_preset_" style="z-index:999991;position:absolute;top:0px;left:0px;width:${width};height:${height};z-index:999999;display:block;">
    <iframe id="_dc_ui_browser_iframe_" style="display:none;border-width:1px;border-style:solid;border-color:lightgray;width:100%;height:100%" frameborder="0"></iframe>
</div>
<img id="_dc_ui_browser_close_" style="z-index:999992;display:none;position:absolute;border-width: 1px;
border-radius: 50%;
border-style: solid;
border-color: white;
margin-top: 20px;width: 36px;height: 36px;" onclick="" src="https://res.dcdev.net/dcdev/v1.0.6/assets/images/close.svg" alt="关闭">
`

            $(`body`).append(html);
            if (data.src.substr(0, 4) == "http") {
                $(`#_dc_ui_browser_iframe_`).attr("src", data.src);
            }
            else {
                $(`#_dc_ui_browser_iframe_`).attr("srcdoc", data.src);
            }
            if (data.is_mask) {
                // 是否使用遮罩
                $(`#_dc_ui_browser_mask_`).css("display", "block").css("opacity", data.mask_opacity).css("width", browser_info.width).css("height", browser_info.height);
                // 阻止事件
                _dc_ui_block_div_events_(`_dc_ui_browser_mask_`);
            }
            $(`#_dc_ui_browser_iframe_`).load(function () {
                // 按百分比
                if (width.indexOf("%") > -1) {

                }
                // 其它单位
                else {
                    var _width = parseInt(width.replace("px", "")) / 2
                    $(`#_dc_ui_browser_container_`).css("left", browser_info.width / 2 - _width);
                }
                // 关闭按钮位置 
                $(`#_dc_ui_browser_close_`).css("left", browser_info.width / 2 - 18).css("top", $(`#_dc_ui_browser_container_`).height());

                $(`#_dc_ui_browser_iframe_`).css("border-width", "0");
                $(`#_dc_ui_browser_iframe_`).css("display", "block");
                $(`#_dc_ui_browser_close_`).css("display", "block")
                ui_class.hide_loading();
            });

            $(`#_dc_ui_browser_back_`).click(function () {
                window.history.back();
            })
            $(`#_dc_ui_browser_forword_`).click(function () {
                window.history.forward();
            })
            $(`#_dc_ui_browser_close_`).click(function () {
                $(`#_dc_ui_browser_mask_`).remove();
                $(`#_dc_ui_browser_container_`).remove();
                $(`#_dc_ui_browser_close_`).remove();
            })
        }
    }
}